package com.one.group.user.service.impl;

import com.mybatisflex.core.tenant.TenantManager;
import com.one.group.model.entity.SpCell;
import com.one.group.result.Result;
import com.one.group.service.SpCellService;
import com.one.group.user.service.CellService;
import com.one.group.utils.ResultUtil;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.time.Duration;

@Service
public class CellServiceImpl implements CellService {
    @Autowired
    private RedisTemplate redisTemplate;
    @DubboReference
    private SpCellService spCellService;
    @Override
    public Result queryAll() {
        //配置忽略多租户
        TenantManager.ignoreTenantCondition();
        return ResultUtil.success(spCellService.list());

    }

    @Override
    public Result queryById(Long id) {
        //先从缓存中获取
        //设置键
        String key = "cell:" + id;
        SpCell spCell = (SpCell) redisTemplate.opsForValue().get(key);
        if (spCell != null){
            return ResultUtil.success(spCell);
        }else {
            //从数据库中获取
            spCell = spCellService.myGetById(id);
            //设置缓存
            redisTemplate.opsForValue().set(key,spCell);
            redisTemplate.expire(key, Duration.ofDays(1));
            return ResultUtil.success(spCell);
        }
    }
}
